//
//  MainViewController.swift
//  LSWeibo1
//
//  Created by 李明禄 on 2018/11/28.
//  Copyright © 2018 SocererGroup. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setController()
        setupCompose()
        
    }
    
    @objc private func editCompose() {
        print("点击了撰写按钮")
    }
    
    lazy var composeBtn: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
}

//撰写微博按钮
extension MainViewController {
    
    private func setupCompose() {
        let composeW = UIScreen.main.bounds.width / CGFloat(self.children.count)
        let composeH = tabBar.bounds.height
        let composeX = 2 * composeW
        composeBtn.frame = CGRect(x: composeX, y: 0, width: composeW, height: composeH)
        
        tabBar.addSubview(composeBtn)
        
        composeBtn.addTarget(nil, action: #selector(editCompose), for: .touchUpInside)
    
    }
    
    
    
}

extension MainViewController {
    
    private func setController() {
        
        let VCDictArray: [[String: String]] = [
            ["clsName": "HomeViewController","title":"首页", "imageName":"home"],
            ["clsName": "DiscoverViewController","title":"发现", "imageName":"discover"],
            ["clsName": ""],
            ["clsName": "MessageViewController","title":"消息", "imageName":"message_center"],
            ["clsName": "ProfileViewController","title":"我的", "imageName":"profile"]
        ]
        
        var controllers = [UIViewController]()
        for dict in VCDictArray {
            controllers.append(dict2VC(dict: dict))
        }
        
        self.viewControllers = controllers
    }
    
    private func dict2VC(dict:[String: String]) -> UIViewController {
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController()
        }
        
        
        let vc = cls.init()
        
        vc.title = title
        
        //使用下面的代码不显示图像，应该放在ASSet中才会用下面的代码
        //        vc.tabBarItem.image = UIImage(contentsOfFile: "tabbar_" + imageName)
        
        //设置tabbar图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_highlighted")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
        //设置tabbar字体及颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15)], for: UIControl.State(rawValue: 0))
        vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange], for: UIControl.State.highlighted)
        
        
        
        
        let nav = LSNavViewController.init(rootViewController: vc)
        
        return nav
        
        
    }
}
